<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/19 0019
 * Time: 下午 15:49
 */
namespace backend\models\c2\entity;

use common\models\c2\entity\AmoebaGroup;
use common\models\c2\statics\AmoebaType;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

class BranchAmoeba extends \common\models\c2\entity\Amoeba
{

    public $branchItems;
    public $parent_id;
    public $amoeba_id;

    public function rules()
    {
        return ArrayHelper::merge(parent::rules(), [
            [['parent_id', 'amoeba_id'], 'required'],
        ]);
    }

    public function getAmoebaGroupJson()
    {
        $root = AmoebaGroup::findOne(['children_amoeba_id' => $this->id]);
        $this->branchItems = AmoebaGroup::findAll(['amoeba_id' => $this->id]);
        // \Yii::info($this->branchItems);
        if (!is_null($root)) {
            $data = [
                'id' => $root->childrenAmoeba->id,
                'name' => $root->childrenAmoeba->label,
                'parent_id' => $root->parent_amoeba_id,
                'children' => $this->getAmoebaGroupChildren($root),
            ];
        } else {
            $data = [
                'id' => $this->id,
                'name' => $this->label,
                'parent_id' => $this->id,
                'children' => [],
            ];
        }

        // Yii::info($data);
        return Json::encode($data);
        // return json_encode($data);
    }

    public function getAmoebaGroupChildren($parent)
    {
        $rec = [];
        foreach ($this->branchItems as $branchItem) {
            if ($branchItem->parent_amoeba_id == $parent->children_amoeba_id) {
                $children = [];
                if ($this->isHasChildren($branchItem)) {
                    $children = $this->getAmoebaGroupChildren($branchItem);
                }
                $rec[] = [
                    'id' => $branchItem->childrenAmoeba->id,
                    'name' => $branchItem->childrenAmoeba->label,
                    'parent_id' => $branchItem->parent_amoeba_id,
                    'children' => $children
                ];
            }
        }
        return $rec;
    }

    public function isHasChildren($parent)
    {
        foreach ($this->branchItems as $branchItem) {
            if ($branchItem->parent_amoeba_id == $parent->children_amoeba_id) {
                return true;
            }
        }
        return false;
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        if ($this->type == AmoebaType::TYPE_BRANCH) {
            $attributes = [
                'amoeba_id' => $this->amoeba_id,
                'children_amoeba_id' => $this->id,
                'parent_amoeba_id' => $this->parent_id,
            ];
            $model = new AmoebaGroup();
            $model->setAttributes($attributes);
            $model->save();
        }
    }

    public function beforeDelete()
    {
        $items = AmoebaGroup::find()->where(['parent_amoeba_id' =>  $this->id])->count();
        if ($items > 0) {
            return false;
        }
        $item = AmoebaGroup::findOne(['children_amoeba_id' => $this->id]);
        $item->delete();
        return parent::beforeDelete(); // TODO: Change the autogenerated stub
    }

}